<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/29
 * Time: 17:10
 */

namespace app\index\controller;

use app\common\entity\RechargeList;
use think\facade\Session;
use app\common\entity\MachineList;
use app\common\entity\Message;
use app\common\entity\MywalletLog;
use app\common\entity\Proportion;
use app\common\entity\UserLevelSend;
use app\common\entity\UserPackage;
use app\common\entity\VoucherList;
use app\common\entity\WithdrawRatio;
use think\Controller;
use think\Db;
use think\Request;
use app\common\entity\User;
use app\common\entity\Market;


class Mywallet extends Base
{

    #获取用户钱包信息
    public function getMywallet()
    {
        $uid = $this->userId;
        $list = \app\common\entity\Mywallet::where('user_id', $uid)->find();
        $list['ks_ratio'] = WithdrawRatio::where('key', 'ky_times')->value('ratio');
        $list['k_ratio'] = WithdrawRatio::where('key', 'k_times')->value('ratio');
        $list['degree_to_cansell'] = WithdrawRatio::where('key', 'degree_to_cansell')->value('ratio');

        if ($list) {
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);
        }
        return json(['code' => 1, 'message' => '获取失败']);

    }

    #可售余额兑换矿池资产(改成矿池钱包)
    public function ksToFz(Request $request)
    {
        $uid = $this->userId;
        $user_info = User::where('id', $this->userId)->find();
        if ($user_info['status'] == -1) {
            return json(['code' => 1, 'message' => '用户已被封禁']);
        }
        $num = $request->post('num');
        $trad_password = $request->post('trad_password');
        $to_freeze_min = WithdrawRatio::where('key', 'to_freeze_min')->value('ratio');
        if ($num < $to_freeze_min) {
            return json(['code' => 1, 'message' => '数量不得小于' . $to_freeze_min]);

        }

        if ($num % 10 != 0) {
            return json(['code' => 1, 'message' => '兑换数量应为10的倍数']);
        }
        $wallet = \app\common\entity\Mywallet::where('user_id', $uid)->find();
        if ($wallet['sell_number'] < $num) {
            return json(['code' => 1, 'message' => '可售余额不足']);
        }

        $user = User::where('id', $this->userId)->find();
        $service = new \app\common\service\Users\Service();
        $result = $service->checkSafePassword($trad_password, $user);
        if (!$result) {
            return json(['code' => 1, 'message' => '支付密码输入错误']);
        }

        $kyRatio = WithdrawRatio::where('key', 'ky_times')->value('ratio');
        $mywalletlog = new MywalletLog();
        $ins_sell_number_log = $mywalletlog->addLog($uid, $num, 'sell_number', '可售余额兑换矿池', 3, 2);
        $ins_freeze_log = $mywalletlog->addLog($uid, $num * $kyRatio, 'freeze', '可售余额兑换矿池钱包', 2, 1);

        $upd_sell_number = \app\common\entity\Mywallet::where('user_id', $uid)->setDec('sell_number', $num);
        $upd_freeze = \app\common\entity\Mywallet::where('user_id', $uid)->setInc('freeze', $num * $kyRatio);
        if ($upd_freeze && $upd_sell_number) {
            return json(['code' => 0, 'message' => '兑换成功']);
        }
        return json(['code' => 1, 'message' => '兑换失败']);

    }

    #信用分兑换可用额度
    public function degreeToCs(Request $request)
    {
        $uid = $this->userId;
        $user_info = User::where('id', $this->userId)->find();
        if ($user_info['status'] == -1) {
            return json(['code' => 1, 'message' => '用户已被封禁']);
        }
        $num = $request->post('num');
        if ($num < 100) {
            return json(['code' => 1, 'message' => '数量不得小于100']);
        }

        if ($num % 100 != 0) {
            return json(['code' => 1, 'message' => '兑换数量应为100的倍数']);
        }
        $trad_password = $request->post('trad_password');

        $wallet = \app\common\entity\Mywallet::where('user_id', $uid)->find();
        if ($wallet['degree_num'] < $num) {
            return json(['code' => 1, 'message' => '信用分不足']);
        }

        $user = User::where('id', $this->userId)->find();
        $service = new \app\common\service\Users\Service();
        $result = $service->checkSafePassword($trad_password, $user);
        if (!$result) {
            return json(['code' => 1, 'message' => '支付密码输入错误']);
        }

        $degree_to_cansell = WithdrawRatio::where('key', 'degree_to_cansell')->value('ratio');
        $mywalletlog = new MywalletLog();
        $can_sell_num = $num / 100 * $degree_to_cansell;
        $change_types = $request->post('limit') ?? 1;
        if ($change_types == 1) {

            $ins_degree_num_log = $mywalletlog->addLog($uid, $num, 'degree_num', '信用分兑换额度', 6, 2);
            $ins_can_sell_log = $mywalletlog->addLog($uid, $can_sell_num, 'fxtc', '信用分兑换额度', 2, 1);
            $updInc = \app\common\entity\Mywallet::where('user_id', $uid)->setInc('fxtc', $can_sell_num);
        } elseif ($change_types == 2) {
            $ins_degree_num_log = $mywalletlog->addLog($uid, $num, 'degree_num', '信用分兑换HMT', 6, 2);
            $ins_can_sell_log = $mywalletlog->addLog($uid, $can_sell_num, 'hmt', '信用分兑换HMT', 9, 1);
            $updInc = \app\common\entity\Mywallet::where('user_id', $uid)->setInc('hmt', $can_sell_num);
        }
        $upd_degree_num = \app\common\entity\Mywallet::where('user_id', $uid)->setDec('degree_num', $num);


        if ($upd_degree_num && $updInc) {
            return json(['code' => 0, 'message' => '兑换成功']);
        }
        return json(['code' => 1, 'message' => '兑换失败']);


    }

    #type == 1消费通证XFTC-->资产通证ZCTC type == 2资产通证ZCTC-->消费通证XFTC
    public function numToFz(Request $request)
    {
        $uid = $this->userId;
        $user_info = User::where('id', $this->userId)->find();
        $type = $request->post('type');
        if ($user_info['status'] == -1) {
            return json(['code' => 1, 'message' => '用户已被封禁']);
        }
        $num = $request->post('num');
        $to_freeze_min = WithdrawRatio::where('key', 'to_freeze_min')->value('ratio');
        if ($num < $to_freeze_min) {
            return json(['code' => 1, 'message' => '数量不得小于' . $to_freeze_min]);

        }
        if ($num % 10 != 0) {
            return json(['code' => 1, 'message' => '数量必须为10的倍数']);

        }

        $wallet = \app\common\entity\Mywallet::where('user_id', $uid)->find();
        $kyRatio = Proportion::where('date', '<=', date('Y-m-d H:i:s', time()))->order('date desc')->value('ratio');
        if ($type == 1) {

            if ($wallet['fxtc'] < $num) {
                return json(['code' => 1, 'message' => 'XFTC余额不足']);
            }
            $ins_num = bcdiv($num, $kyRatio, 5);
        } else {
            if ($wallet['zctc'] < $num) {
                return json(['code' => 1, 'message' => 'ZCTC余额不足']);
            }
            $ins_num = bcmul($num, $kyRatio, 5);
        }

        $trad_password = $request->post('trad_password');
        $user = User::where('id', $this->userId)->find();
        $service = new \app\common\service\Users\Service();
        $result = $service->checkSafePassword($trad_password, $user);
        if (!$result) {
            return json(['code' => 1, 'message' => '支付密码输入错误']);
        }

        $mywalletlog = new MywalletLog();
        if ($type == 1) {

            $ins_sell_number_log = $mywalletlog->addLog($uid, $num, 'fxtc', '消费通证XFTC->资产通证ZCTC', 2, 2);
            $ins_freeze_log = $mywalletlog->addLog($uid, $ins_num, 'zctc', '消费通证XFTC->资产通证ZCTC', 1, 1);

            $upd_number = \app\common\entity\Mywallet::where('user_id', $uid)->setDec('fxtc', $num);
            $upd_freeze = \app\common\entity\Mywallet::where('user_id', $uid)->setInc('zctc', $ins_num);
        } else {

            $ins_sell_number_log = $mywalletlog->addLog($uid, $ins_num, 'fxtc', '资产通证ZCTC->消费通证XFTC', 2, 1);
            $ins_freeze_log = $mywalletlog->addLog($uid, $num, 'zctc', '资产通证ZCTC->消费通证XFTC', 1, 2);

            $upd_number = \app\common\entity\Mywallet::where('user_id', $uid)->setInc('fxtc', $ins_num);
            $upd_freeze = \app\common\entity\Mywallet::where('user_id', $uid)->setDec('zctc', $num);

        }
        if ($upd_freeze && $upd_number) {
            return json(['code' => 0, 'message' => '兑换成功']);
        }
        return json(['code' => 1, 'message' => '兑换失败']);


    }


    #财务记录
    public function myWalletLog(Request $request)
    {
        $uid = $this->userId;
        $list = MywalletLog::where('user_id', $uid)
            ->where('types', $request->post('type'))
            ->page($request->post('page'))
            ->limit($request->post('limit'))
            ->order('create_time desc,new desc')
            ->select();
        foreach ($list as &$v) {
            $v['number'] = $v['number'] * 100000 / 100000;
            $v['new'] = $v['new'] * 100000 / 100000;
        }
        $count = MywalletLog::where('user_id', $uid)
            ->where('types', $request->post('types'))->count();
        if ($list) {
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list, 'count' => $count]);
        }
        return json(['code' => 1, 'message' => '获取失败']);

    }


    #矿机列表
    public function machineList()
    {
        $uid = $this->userId;
        $list = MachineList::where('m.user_id', $uid)
            ->alias('m')
            ->field('m.*,p.num as day_get')
            ->leftJoin('user_package p', 'm.level = p.level')
            ->select();
        foreach ($list as &$v) {
            $v['hour_get'] = $v['day_get'] / 24;
            $end_time_day = UserPackage::where('level', $v['level'])->value('end_time');
            $v['end_time'] = $end_time_day * 24;
            $v['use_time'] = floor((time() - strtotime($v['create_time'])) / 3600);
            $v['next_time'] = date('Y-m-d H:i:s', $v['get_time'] + 3600 * 24);
            if (time() - $v['get_time'] > 3600 * 24) {

                $v['today_get'] = 1;
                $v['is_24'] = 2;
            }
        }
        if ($list) {
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);
        }
        return json(['code' => 1, 'message' => '获取失败']);

    }

    #矿机列表过期
    public function machineList_no()
    {
        $uid = $this->userId;
        $list = MachineList::where('m.user_id', $uid)->where('m.status', 2)
            ->alias('m')
            ->field('m.*,p.num as day_get')
            ->leftJoin('user_package p', 'm.level = p.level')
            ->select();
        foreach ($list as &$v) {
            $v['hour_get'] = $v['day_get'] / 24;
            $end_time_day = UserPackage::where('level', $v['level'])->value('end_time');
            $v['end_time'] = $end_time_day * 24;
            $v['use_time'] = floor((time() - strtotime($v['create_time'])) / 3600);

        }
        if ($list) {
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);
        }
        return json(['code' => 1, 'message' => '获取失败']);

    }

    #矿机详情
    public function machineInfo(Request $request)
    {
        $machine_id = $request->post('id');
        $list = MachineList::where('id', $machine_id)->find();
        $end_time_day = UserPackage::where('level', $list['level'])->value('end_time');
        $day_get = UserPackage::where('level', $list['level'])->value('num');
        $miao_get = $day_get / 24 / 3600;

        $end_time_hour = $end_time_day * 24;
        $use_time = time() - strtotime($list['create_time']);
        $can_get_time = time() - $list['get_time'];
        $hour = floor($use_time / 3600);
        $list['end_hour'] = $end_time_hour;
        $list['use_time'] = $hour;
        $list['miao_get'] = $miao_get;
        $list['total_num'] = $list['get_num'] + ($miao_get * $can_get_time);
        if ($list) {
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);
        }
        return json(['code' => 1, 'message' => '获取失败']);
    }

    #领取单个矿机
    public function getOneMachine(Request $request)
    {
        $machine_id = $request->post('id');
        $uid = $this->userId;
        if (!$machine_id || !$uid) {
            return json(['code' => 1, 'message' => '参数错误']);
        }
        $machineInfo = MachineList::where('id', $machine_id)->find();
        if ($machineInfo['user_id'] != $uid) {
            return json(['code' => 1, 'message' => '用户尚未登录,领取失败']);
        }
        $user_info = User::where('id', $uid)->find();
        if ($user_info['status'] == -1) {
            return json(['code' => 1, 'message' => '用户已被封禁']);
        }
        (new \app\common\entity\Mywallet())->updMachineGet($uid);

        if ($machineInfo['status'] != 1) {
            return json(['code' => 1, 'message' => '矿机已过期']);
        }
        if ($machineInfo['today_get'] != 1) {
            return json(['code' => 1, 'message' => '今日已领取']);
        }
        if ($machineInfo['is_24'] != 2) {
            return json(['code' => 1, 'message' => '未满24小时，请稍后领取']);
        }
        $day_get = UserPackage::where('level', $machineInfo['level'])->value('num');
        $res = $this->getMachineNumber($uid, $day_get);
        if ($res) {
            $share = (new \app\common\entity\Mywallet())->share($uid, $day_get);
            $upd = MachineList::where('id', $machineInfo['id'])->update(['today_get' => 2, 'can_get_num' => 0, 'is_24' => 1, 'get_time' => time()]);
            $updtotal = MachineList::where('id', $machineInfo['id'])->setInc('get_num', $day_get);
            return json(['code' => 0, 'message' => '领取成功,领取了' . $day_get . '个产币']);
        }
        return json(['code' => 1, 'message' => '领取失败']);


    }

    #点击获取矿机资产(福多多一键领取)
    public function indexchickMachine()
    {

        $chick_num = 0;
        $uid = $this->userId;
        $user_info = User::where('id', $this->userId)->find();
        if ($user_info['status'] == -1) {
            return json(['code' => 1, 'message' => '用户已被封禁']);
        }
        $list = MachineList::where('user_id', $uid)
            ->where('status', 1)
            ->where('get_time', '<', time() - 3600 * 24)
            ->select();
        $total_get = 0;
        foreach ($list as $v) {
            $machine_package = UserPackage::where('level', $v['level'])->find();
            $day_get = $machine_package['num'];
            if ($v['get_num'] >= $machine_package['total_num']) {
                //过期
                $updStatus = Db('machine_list')->where('id', $v['id'])->update(['status' => 2]);
                return false;
            }
            if ((time() - strtotime($v['create_time'])) >= ($machine_package['end_time'] * 24 * 3600)) {
                //过期
                $updStatus = MachineList::where('id', $v['id'])->update(['status' => 2]);

                return false;
            }
            $res = $this->getMachineNumber($uid, $day_get);
            if ($res) {
                $upd = MachineList::where('id', $v['id'])->update(['today_get' => 2, 'can_get_num' => 0, 'is_24' => 1, 'get_time' => time()]);
                $updtotal = MachineList::where('id', $v['id'])->setInc('get_num', $day_get);
                $total_get = $day_get + $total_get;
                if ($upd) {
                    $chick_num++;
                }
            }
        }
        if ($chick_num == '0') {
            return json(['code' => 1, 'message' => '未满24小时，请稍后领取！']);

        }
        if ($total_get > 0) {
            $share = (new \app\common\entity\Mywallet())->share($uid, $total_get);

        }

        return json(['code' => 0, 'message' => '产生' . $total_get . '个币']);

    }

    #点击获取矿机资产
    public function chickMachine(Request $request)
    {

        $chick_num = 0;
        $uid = $this->userId;
        $user_info = User::where('id', $this->userId)->find();
        if ($user_info['status'] == -1) {
            return json(['code' => 1, 'message' => '用户已被封禁']);
        }
        (new \app\common\entity\Mywallet())->updMachineGet($uid);
        $list = MachineList::where('user_id', $uid)
            ->where('status', 1)
            ->where('today_get', 1)
            ->where('is_24', 2)
            ->select();
        $total_get = 0;
        foreach ($list as $v) {
            $day_get = UserPackage::where('level', $v['level'])->value('num');
//            $run_time_num = floor((time() - strtotime($v['create_time'])) / (3600 * 24)) * $day_get;
//            $total_can_get_num = $run_time_num - $v['get_num'];
            $res = $this->getMachineNumber($uid, $day_get);
            if ($res) {
                $upd = MachineList::where('id', $v['id'])->update(['today_get' => 2, 'can_get_num' => 0, 'is_24' => 1, 'get_time' => time()]);
                $updtotal = MachineList::where('id', $v['id'])->setInc('get_num', $day_get);
                $total_get = $day_get + $total_get;
                if ($upd) {
                    $chick_num++;
                }
            }
        }
        if ($chick_num == '0') {
            return json(['code' => 1, 'message' => '未满24小时，请稍后领取！']);

        }
        if ($total_get > 0) {
            $share = (new \app\common\entity\Mywallet())->share($uid, $total_get);

        }

        return json(['code' => 0, 'message' => '领取' . $chick_num . '台矿机收益']);

    }

    #更新获取矿机钱包资产

    public function getMachineNumber($uid, $day_get)
    {
        if ($day_get > 0) {

            $insLog = (new MywalletLog())->addLog($uid, $day_get, 'sell_number', '矿机产币', 3, 1);
            $upd = \app\common\entity\Mywallet::where('user_id', $uid)->setInc('sell_number', $day_get);
            return true;
        }
        return false;

    }

    #是否登录

    public function is_login(Request $request)
    {
        $uid = $this->userId;
        $userInfo = User::where('id', $uid)->find();
        $login_time = $userInfo['login_time'];
        $session_login_time = Session::get('login_time');
        if ($userInfo['is_close'] == -1) {
            return json(['code' => 1, 'message' => '用户已被永久封禁', 'url' => 'login']);
        }
        if ($login_time != $session_login_time) {
            return json(['code' => 1, 'message' => '账号在别处登录,强制退出', 'url' => 'login']);
        }
        if ($uid) {
            return json(['code' => 0, 'message' => 'yes', 'info' => $uid]);
        }
        return json(['code' => 1, 'message' => '暂未登录', 'url' => 'login']);

    }


    public function aaabbb()
    {
        // $userModel = new User();
        // $res = Db::table('user_invite_code')->where('invite_code',0)->find();

        // $res = session_destroy();
        // dump($res);
        // die;
        dump(1);
        die;

        $a = Db::table('user')
            ->where('real_name', '')->limit(100)
            ->select();
        $res = [];
        foreach ($a as $k => $vv) {
            $res[$k]['id'] = $vv['id'];
            $res[$k]['user_invite_code'] = Db::table('user_invite_code')->where('user_id', $vv['id'])->select();
            $res[$k]['user'] = Db::table('user')->where('id', $vv['id'])->select();
            $res[$k]['my_wallet'] = Db::table('my_wallet')->where('user_id', $vv['id'])->select();
            $res[$k]['has_machine'] = Db::table('has_machine')->where('user_id', $vv['id'])->select();
            $res[$k]['unsealing_list'] = Db::table('unsealing_list')->where('user_id', $vv['id'])->select();
            $res[$k]['my_wallet_log'] = Db::table('my_wallet_log')->where('user_id', $vv['id'])->select();
            $res[$k]['market_list'] = Db::table('market_list')->where('user_id', $vv['id'])->select();
            $res[$k]['machine_list'] = Db::table('machine_list')->where('user_id', $vv['id'])->select();
        }
        // $tui_3 = $userModel->getChildsInfoId(10000);
        //    $result = array_reduce($tui_3, function ($result, $value) {
        //            return array_merge($result, array_values($value));
        //        }, array());
        // $tui_32 = $userModel->getChildsInfo(10690, 4);
        dump($res);
        die;
        return json(['1' => $a, '2' => $tui_32, '3' => $result]);
    }

    #发送请求
    public function http_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    #获取一条最新行情
    public function getNewHashRatio(Request $request)
    {
        $list = Proportion::where('date', '<=', date('Y-m-d H:i:s', time()))->order('date desc')->find();
        $buy_list = WithdrawRatio::where('key', 'buy_set_num')->value('ratio');
        $usdt_ratio = WithdrawRatio::where('key', 'usdt_ratio')->value('ratio');
        $can_buy = explode(",", $buy_list);



        if ($request->post('types') == 2) {
            $list['ratio'] = 1;
        }

        if ($list) {
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list, 'can_buy_list' => $can_buy]);
        }
        return json(['code' => 1, 'message' => '获取失败']);
    }

    #获取代币行情
    public function getHashRatio(Request $request)
    {
        $info['today'] = Proportion::where('date', '<=', date('Y-m-d'))->order('date desc')->value('ratio');
        $info['yesterday'] = Proportion::where('date', '<', date('Y-m-d'))->order('date desc')->value('ratio');
        $info['upordown'] = bcmul(($info['today'] - $info['yesterday'])/$info['yesterday'],100,2);

        //今日交易手续费
        $todat_market_charges = Market::whereTime('end_time','today')->where('status',4)->field('sum(charges*num) as market_charges')->find();
        $info['today_market_charges'] = intval($todat_market_charges['market_charges']);

        //总交易手续费
        $market_charges = Market::where('end_time','>',0)->where('status',4)->field('sum(charges*num) as market_charges')->find();
        $info['market_charges'] = intval($market_charges['market_charges']);
        //平台当前求购量
        $info['market_buy'] = Market::where('status',1)->where('types',1)->sum('num');
        //平台今日出售量
        $info['today_market_sell'] = Market::where('status',1)->where('types',2)->sum('num');
        //昨日成交量
        $info['yesterday_market_num'] = Market::whereTime('end_time','yesterday')->where('status',4)->sum('num');
        //今日成交量
        $info['today_market_num'] = Market::whereTime('end_time','today')->where('status',4)->sum('num');
        $info['fbb'] = \app\common\entity\Mywallet::where('user_id',$this->userId)->value('fbb');

        return json(['code' => 0, 'message' => '获取成功', 'info' =>$info]);
        return json(['code' => 1, 'message' => '获取失败']);
    }

    #每日获取矿池
    public function everydayGetFreeze()
    {
        $uid = $this->userId;
        $user_info = User::where('id', $this->userId)->find();
        if ($user_info['status'] == -1) {
            return json(['code' => 1, 'message' => '用户已被封禁']);
        }

        $hash_time1 = User::where('id', $uid)->value('hash_time');
        $nowtime = date('Ymd', time());
        $hash_time = date('Ymd', $hash_time1);
        $tomorrow = date('Y-m-d H:i:s', $hash_time1 + 24 * 3600);
        if ($nowtime == $hash_time) {

            return json(['code' => 1, 'message' => '1天只能释放一次，请明天再试！']);

        }
        $freeze_ratio = WithdrawRatio::where('key', 'k_everyday')->value('ratio');
        $freeze = \app\common\entity\Mywallet::where('user_id', $uid)->value('freeze');
        if ($freeze <= 0) {
            return json(['code' => 1, 'message' => '矿池不足']);

        }
        $down_freeze = $freeze * $freeze_ratio;
        if ($down_freeze <= '0') {
            return json(['code' => 1, 'message' => '暂无释放']);

        }
        $mywalletLog = new MywalletLog();
        $insFzLog = $mywalletLog->addLog($uid, $down_freeze, 'freeze', '每日释放', 2, 2);
        $insSellLog = $mywalletLog->addLog($uid, $down_freeze, 'sell_number', '每日释放', 3, 1);
        $updFz = \app\common\entity\Mywallet::where('user_id', $uid)->setDec('freeze', $down_freeze);
        $updSell = \app\common\entity\Mywallet::where('user_id', $uid)->setInc('sell_number', $down_freeze);
        $updSell1 = \app\common\entity\Mywallet::where('user_id', $uid)->setInc('voucher_num', $down_freeze);
        $updTime = User::where('id', $uid)->update(['hash_time' => time()]);
        if ($updFz && $updSell) {
            return json(['code' => 0, 'message' => '今日释放' . $down_freeze]);

        }
        return json(['code' => 1, 'message' => '获取失败']);

    }

    #申述
    public function upMessage(Request $request)
    {
        $img = $request->post('img');
        $uid = $this->userId;
        $is_up = Message::where('user_id', $uid)->where('market_id', $request->post('market_id'))->find();
        if ($is_up) {
            return json(['code' => 1, 'message' => '改交易已提交申述,请耐心等待']);
        }
        $ins = Message::insert(['user_id' => $uid, 'content' => $request->post('content'), 'market_id' => $request->post('market_id'), 'create_time' => time(), 'img' => $img]);
        if ($ins) {
            \app\common\entity\Market::where('id', $request->post('market_id'))->setField('status', '9');
            return json(['code' => 0, 'message' => '提交成功']);
        }
        return json(['code' => 1, 'message' => '提交失败']);
    }

    #获取平台支付宝收款码
    public function ptZfb()
    {
        $list = Db::table('image')->where('id', 2)->value('pic');

        if ($list) {
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);
        }
        return json(['code' => 1, 'message' => '获取失败']);
    }


    #用户之间互转
    public function UserToUser(Request $request)
    {
        $uid = $this->userId;

        $user_can_sell = WithdrawRatio::where('key', 'user_can_sell')->value('ratio');
        if ($user_can_sell == 0) {
            return json(['code' => 1, 'message' => '用户互转已关闭']);

        }

        $num = $request->post('num');
        $mobile = $request->post('mobile');
        if (!$num || !$mobile) {
            return json(['code' => 1, 'message' => '参数错误']);

        }
        $trad_password = $request->post('trad_password');


        $user_to_min = WithdrawRatio::where('key', 'user_to_min')->value('ratio');
        $user_to_max = WithdrawRatio::where('key', 'user_to_max')->value('ratio');
        $ratio = WithdrawRatio::where('key', 'user_to_user')->value('ratio');
        $ratioNum = $num + $num * $ratio;
        if ($num < $user_to_min) {
            return json(['code' => 1, 'message' => '互转数量不能小于' . floatval($user_to_min)]);

        }
        $mywalletInfo = \app\common\entity\Mywallet::where('user_id', $uid)->find();

        if ($mywalletInfo['sell_number'] < $ratioNum) {
            return json(['code' => 1, 'message' => '可售猪猪不足']);
        }
        if ($mywalletInfo['can_sell'] < $num) {
            return json(['code' => 1, 'message' => '可售额度不足']);
        }

        $user = User::where('id', $this->userId)->find();
        $service = new \app\common\service\Users\Service();
        $result = $service->checkSafePassword($trad_password, $user);
        if (!$result) {
            return json(['code' => 1, 'message' => '支付密码输入错误']);
        }

        $user_mobile = User::where('id', $uid)->value('mobile');
        $for_user_id = User::where('money_address', $mobile)->value('id');
        if (!$for_user_id) {
            return json(['code' => 1, 'message' => '用户不存在']);

        }

        $mywalletLog = new MywalletLog();
        $ky_num_times = WithdrawRatio::where('key', 'ky_num_times')->value('ratio');
        $insSellNumLog = $mywalletLog->addLog($uid, $ratioNum, 'sell_number', '转给' . $mobile, 3, 2);
        $insCanSellLog = $mywalletLog->addLog($uid, $num, 'can_sell', '转给' . $mobile, 4, 2);
        $insforNumberLog = $mywalletLog->addLog($for_user_id, $num, 'number', '来自' . $user_mobile, 1, 1);
        $insforCanSellLog = $mywalletLog->addLog($for_user_id, $num * $ky_num_times, 'can_sell', '来自' . $user_mobile, 4, 1);
        $updSellNum = \app\common\entity\Mywallet::where('user_id', $uid)->setDec('sell_number', $ratioNum);
        $updCanSell = \app\common\entity\Mywallet::where('user_id', $uid)->setDec('can_sell', $num);
        $updforNumberNum = \app\common\entity\Mywallet::where('user_id', $for_user_id)->setInc('number', $num);
        $updforCanSellNum = \app\common\entity\Mywallet::where('user_id', $for_user_id)->setInc('can_sell', $num * $ky_num_times);

        if ($updforNumberNum && $updforCanSellNum && $updSellNum && $updCanSell) {
            return json(['code' => 0, 'message' => '转出成功']);
        }
        return json(['code' => 1, 'message' => '转出失败']);

    }


    public function withdraw()
    {

    }


    #充币地址
    public function chargeAddress()
    {
        $hmt_address = WithdrawRatio::where('key', 'hmt_address')->value('ratio');
        $usdt_address = WithdrawRatio::where('key', 'usdt_address')->value('ratio');
        if ($hmt_address && $usdt_address) {

            return json(['code' => 0, 'message' => '获取成功', 'hmt' => $hmt_address, 'usdt' => $usdt_address]);
        }
        return json(['code' => 1, 'message' => '获取失败']);

    }

    #充币
    public function addChargeList(Request $request)
    {
        $num = $request->post('num');
        $hash = $request->post('hash');
        if ($num <= 0) {
        	return json(['code' => 1, 'message' => '数量不正确']);
        	
        }
        if (!$hash) {
        	return json(['code' => 1, 'message' => '请输入区块哈希']);
        	
        }
        $money_types = $request->post('money_types');

        $rechargeList = new RechargeList();
        $rechargeList->startTrans();

        $rechargeList->user_id = $this->userId;
        $rechargeList->num = $num;
        // $rechargeList->img = $img;
        $rechargeList->hash = $hash;
        $rechargeList->types = 1;
        $rechargeList->status = 1;
        $rechargeList->money_types = $money_types;
        $rechargeList->create_time = time();

        $res = $rechargeList->save();

        if ($res) {
            $rechargeList->commit();
            return json(['code' => 0, 'message' => '提交成功']);
        }
        $rechargeList->rollback();
        return json(['code' => 1, 'message' => '提交失败']);

    }


    #提币
    public function addWithdrawList(Request $request)
    {
        $trans_ratio = WithdrawRatio::where('key', 'trans_ratio')->value('ratio');
        if ($request->isPost()) {

            $num = $request->post('num');
            $trad_password = $request->post('trad_password');
            $address = $request->post('address');
            $money_types = $request->post('money_types');
            if (!$num || !$trad_password || !$address || !$money_types) {
                return json(['code' => 1, 'message' => '缺少参数']);

            }

            if ($num<= 0) {
                return json(['code' => 1, 'message' => '数量不正确']);

            }
            $ratio_num = bcmul($num, $trans_ratio, 5);
//            $num = $num + $ratio_num;//加了矿工费后

            $mywalletInfo = \app\common\entity\Mywallet::where('user_id', $this->userId)->find();
            $user = User::where('id', $this->userId)->find();
            $service = new \app\common\service\Users\Service();
            $result = $service->checkSafePassword($trad_password, $user);
            if (!$result) {
                return json(['code' => 1, 'message' => '支付密码输入错误']);
            }

            if ($mywalletInfo['qytc'] < $ratio_num) {
                return json(['code' => 1, 'message' => 'QYTC不足']);

            }
            $mywalletLogModel = new MywalletLog();

            if ($money_types == 1) {
                if ($mywalletInfo['zctc'] < $num) {
                    return json(['code' => 1, 'message' => '账户余额不足']);

                }

                $mywalletLogModel->addLog($this->userId, $num, 'zctc', '提币(矿工费:' . $ratio_num . ')', 1, 2);
                \app\common\entity\Mywallet::where('user_id', $this->userId)->setDec('zctc', $num);

                $mywalletLogModel->addLog($this->userId, $ratio_num, 'qytc', '提币矿工费', 3, 2);
                \app\common\entity\Mywallet::where('user_id', $this->userId)->setDec('qytc', $ratio_num);
            } elseif ($money_types == 2) {
                if ($mywalletInfo['usdt'] < $num) {
                    return json(['code' => 1, 'message' => '账户余额不足']);

                }

                $mywalletLogModel->addLog($this->userId, $num, 'usdt', '提币(矿工费:' . $ratio_num . ')', 8, 2);
                \app\common\entity\Mywallet::where('user_id', $this->userId)->setDec('usdt', $num);

                $mywalletLogModel->addLog($this->userId, $ratio_num, 'qytc', '提币矿工费', 3, 2);
                \app\common\entity\Mywallet::where('user_id', $this->userId)->setDec('qytc', $ratio_num);
            }


            $rechargeList = new RechargeList();
            $rechargeList->startTrans();

            $rechargeList->user_id = $this->userId;
            $rechargeList->num = $num;
            $rechargeList->address = $address;
            $rechargeList->types = 2;
            $rechargeList->status = 1;
            $rechargeList->ratio = $ratio_num;
            $rechargeList->money_types = $money_types;
            $rechargeList->create_time = time();

            $res = $rechargeList->save();

            if ($res) {
                $rechargeList->commit();
                return json(['code' => 0, 'message' => '提交成功']);
            }
            $rechargeList->rollback();
            return json(['code' => 1, 'message' => '提交失败']);
        }
        if ($request->isGet()) {
            $trans_ratio = $trans_ratio * 100;
            return json(['code' => 0, 'message' => '提交成功', 'info' => $trans_ratio]);
        }

    }

    #提币充币记录
    public function rechargeLog(Request $request)
    {
        $list = RechargeList::where('user_id', $this->userId)
            ->where('types', $request->post('types'))
            ->limit($request->post('limit'))
            ->page($request->post('page'))
            ->order('create_time desc')
            ->select();
        $count = RechargeList::where('user_id', $this->userId)
            ->where('types', $request->post('types'))
            ->count();
        if ($list) {
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list, 'count' => $count]);
        }
        return json(['code' => 1, 'message' => '暂无数据']);

    }

    #提币充币记录
    public function moneyWalletLog(Request $request)
    {
        $list = RechargeList::where('user_id', $this->userId)
//            ->where('types', $request->post('types'))
            ->limit($request->post('limit'))
            ->page($request->post('page'))
            ->order('create_time desc')
            ->select();
        $count = RechargeList::where('user_id', $this->userId)
//            ->where('types', $request->post('types'))
            ->count();
        if ($list) {
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list, 'count' => $count]);
        }
        return json(['code' => 1, 'message' => '暂无数据']);

    }

    #钱包页面财务记录(不用)
    public function moneyWalletLog111(Request $request)
    {
        $uid = $this->userId;
        $list = MywalletLog::where('user_id', $uid)
            ->whereIn('types', '9,10')
            ->page($request->post('page'))
            ->limit($request->post('limit'))
            ->order('create_time desc,new desc')
            ->select();
        $count = MywalletLog::where('user_id', $uid)
            ->whereIn('types', '9,10')
            ->count();
        if ($list) {
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list, 'count' => $count]);
        }
        return json(['code' => 1, 'message' => '获取失败']);

    }


    public function moneyPackage()
    {
        $uid = $this->userId;
        $mywallet = \app\common\entity\Mywallet::where('user_id', $uid)->find();
        $usdt_ky = $mywallet['usdt'];
        $hmt_ky = $mywallet['zctc'];
        $hmt_fz = RechargeList::where('user_id', $uid)->where('types', 2)->where('status', 1)->where('money_types', 1)->sum('num');
        $usdt_fz = RechargeList::where('user_id', $uid)->where('types', 2)->where('status', 1)->where('money_types', 2)->sum('num');
        $list['hmt_fz'] = $hmt_fz;
        $list['hmt'] = $hmt_fz + $hmt_ky;
        $list['hmt_ky'] = $hmt_ky + 0;
        $list['usdt_ky'] = $usdt_ky + 0;
        $list['usdt_fz'] = $usdt_fz;
        $list['usdt'] = $usdt_ky + $usdt_fz;
        return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);
    }

}